package com.ruicar.afs.cloud.manage.creditchange.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.config.ContractFeginConfig;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractFeginParamEnum;
import com.ruicar.afs.cloud.manage.api.dto.SmallprogramDto;
import com.ruicar.afs.cloud.manage.api.feign.applyfeign.ApplySmallprogramFeign;
import com.ruicar.afs.cloud.manage.common.entity.ManageBankCardChange;
import com.ruicar.afs.cloud.manage.common.service.ComAttachementFileService;
import com.ruicar.afs.cloud.manage.common.service.ManageBankCardChangeService;
import com.ruicar.afs.cloud.manage.creditchange.condition.TencentCloudNuclearBodyBankCondition;
import com.ruicar.afs.cloud.manage.creditchange.service.BankApproveService;
import com.ruicar.afs.cloud.wechat.dto.WechatDTO;
import com.ruicar.afs.cloud.wechat.service.WechatService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author chuke
 * @create 2020/9/7 15:40
 */
@Slf4j
@Service
@AllArgsConstructor
public class BankApproveServiceImpl implements BankApproveService {
    private final StringRedisTemplate stringRedisTemplate;
    private final WechatService wechatService;
    private final ApplySmallprogramFeign applySmallprogramFeign;
    private final ContractFeginConfig contractFeginConfig;
    private final ComAttachementFileService comAttachementFileService;
    private final ManageBankCardChangeService manageBankCardChangeService;
    public static final String MESSAGE_TYPE_ZERO = "changeBankInfo";

    @Override
    public Map getSmallProgramQrCode(TencentCloudNuclearBodyBankCondition condition) {
        WechatDTO wechatDTO = new WechatDTO();
        Map<String, String> stringMap = new HashMap<>(4);
        String uuId = UUID.randomUUID().toString().replaceAll("-", "");
        wechatDTO.setScene(uuId);
        log.info("redis缓存key:" + uuId);
        wechatDTO.setWidth(150);
        BASE64Encoder encoder = new BASE64Encoder();
        Map token = this.getToken();
        JSONObject json = new JSONObject();
        //redis缓存key
        if (MESSAGE_TYPE_ZERO.equals(condition.getSignScene())) {
            //预审批表主键
//            json.put("preId", condition.getPreId().toString());
        } else {
            //客户信息表主键、申请编号
            json.put("applyNo", condition.getCaseNo());
//            json.put("custId", condition.getCustId().toString());
        }
        //存数据于缓存中
        stringRedisTemplate.opsForValue().set(uuId, json.toJSONString(), 30, TimeUnit.MINUTES);
        String encodeBuffer = encoder.encodeBuffer(wechatService.getScene(wechatDTO, token)).trim();
        log.info("二维码-String:{}", encodeBuffer);
        stringMap.put("code", encodeBuffer);
        stringMap.put("uuId", uuId);
        stringMap.put("activeTime", token.get("activeTime").toString());

        //将数据存到进件
        Map<String, String> heards = new HashMap<>();
        heards.put(AfsEnumUtil.key(ContractFeginParamEnum.APPLY_CLIENT_ID), contractFeginConfig.getApplyClientId());
        heards.put(AfsEnumUtil.key(ContractFeginParamEnum.APPLY_CLIENT_SECRET), contractFeginConfig.getApplyClientSecret());
        SmallprogramDto dto = new SmallprogramDto();
        ManageBankCardChange bankCardChange = manageBankCardChangeService.getOne(Wrappers.<ManageBankCardChange>lambdaQuery()
                .eq(ManageBankCardChange::getCaseNo, condition.getCaseNo()));
        dto.setId(bankCardChange.getId());
        dto.setCaseNo(condition.getCaseNo());
        dto.setKeyUuId(wechatDTO.getScene());
        dto.setSignScene(condition.getSignScene());
        applySmallprogramFeign.getSmallprogramInfo(dto, heards);
        return stringMap;
    }

    /**
     * 获取微信二维码token
     *
     * @return
     */
    @Override
    public Map getToken() {
        Map<String, String> map = new HashMap(4);
        Map<String, String> header = new HashMap<>(4);
        header.put("grant_type", "client_credential");
        header.put("appid", "wxdccf9f85a15b3173");
        header.put("secret", "42274ebf1a5a339f10d7c1da1092dc1a");
        JSONObject token = this.wechatService.getToken(header);
        map.put("access_token", token.get("access_token").toString());
        map.put("activeTime", token.get("expires_in").toString());
        return map;
    }

    /**
     * 二维码信息封装
     *
     * @param condition
     * @return
     */
    @Override
    public Map getQrCode(TencentCloudNuclearBodyBankCondition condition) {
        condition.setSignScene(MESSAGE_TYPE_ZERO);
        Map qrCode = getSmallProgramQrCode(condition);
        return qrCode;
    }


}
